package com.ebelter.bodyfatscale.ui.activity;

import android.text.Editable;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.ebelter.bodyfatscale.R;
import com.ebelter.bodyfatscale.common.AppDateLitConstants;
import com.ebelter.bodyfatscale.common.Constants;
import com.ebelter.bodyfatscale.moudules.https.NetUtils;
import com.ebelter.bodyfatscale.moudules.https.interfaces.HttpResponse;
import com.ebelter.bodyfatscale.moudules.https.response.HttpsParameters;
import com.ebelter.bodyfatscale.moudules.https.response.OriginalParametersResponse;
import com.ebelter.bodyfatscale.util.MyTextWatcher;
import com.ebelter.bodyfatscale.util.NumUtils;
import com.ebelter.bodyfatscale.util.ToastUtil;
import com.ebelter.bodyfatscale.util.UserSpUtil;
import com.ebelter.btlibrary.btble.impl.scale.model.ScaleUser;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import butterknife.BindView;
import butterknife.OnClick;

//修改身高界面
public class ModifyHeightActivity extends BaseActivity {


    public static String ORIGINAL_HEIGHT = "ORIGINAL_HEIGHT";
    public static final int REQUESTCODE = 107;


    @BindView(R.id.modifyheight_back_bt)
    ImageView modifyheight_back_bt;

    @BindView(R.id.et_height_pager)
    EditText et_height_pager;

    @BindView(R.id.modifyheight_done_tv)
    TextView modifyheight_done_tv;

    @Override
    protected int loadLayoutById() {
        return R.layout.activity_modifyheight;
    }

    @Override
    protected void initViews() {

    }

    int originalHeight;
    @Override
    protected void initData() {

        originalHeight = getIntent().getIntExtra(ORIGINAL_HEIGHT,-1);
        if (originalHeight==-1){
            finish();
        }
        et_height_pager.setText(originalHeight+"");
        et_height_pager.addTextChangedListener(new MyTextWatcher() {
            @Override
            public void afterTextChanged(Editable s) {
                String height=s.toString();
                if (TextUtils.isEmpty(height)){
                    modifyheight_done_tv.setEnabled(false);
                }else {
                    modifyheight_done_tv.setEnabled(true);
                }
//                int h = NumUtils.string2Int(height);
//                if (h< AppDateLitConstants.HEIGHT_MIN||h>AppDateLitConstants.HEIGHT_MAX){
//                    ToastUtil.show(R.string.Please_enter_a_value_between_20_and_250_height);
//                    ViewUtils.shakeView(et_height_pager);
//                    modifyheight_done_tv.setEnabled(false);
//                }else {
//                    modifyheight_done_tv.setEnabled(true);
//                }
            }
        });

    }


    @OnClick({ R.id.modifyheight_back_bt,R.id.modifyheight_done_tv})
    public void btClick(View view){
        switch (view.getId()){
            case R.id.modifyheight_back_bt:
                finish();
                break;
            case R.id.modifyheight_done_tv:
               // ToastUtil.show(modify_ed.getText().toString());
                done();
                break;


        }
    }

    private void done() {

        String height=et_height_pager.getText().toString().trim();
        int h = NumUtils.string2Int(height);
        if (h< AppDateLitConstants.HEIGHT_MIN||h>AppDateLitConstants.HEIGHT_MAX){
            ToastUtil.show(R.string.Please_enter_a_value_between_20_and_250_height);
            return;
        }
        if (h==originalHeight){
            ToastUtil.show(R.string.Modify_Success);
            finish();
            return;
        }

        if (!NetUtils.available()){
            ToastUtil.show(R.string.Network_error);
            return;
        }
        Map<String,Serializable> paramMap=new HashMap<>();
        paramMap.put(HttpsParameters.USER_ID, UserSpUtil.readLong(Constants.IUser.USER_ID));
        paramMap.put(HttpsParameters.HEIGHT,h);
        NetUtils.getInstance().updateInfo(this, paramMap, new HttpResponse<OriginalParametersResponse>() {
            @Override
            public void result(boolean isRequestSuccess, String requestDes, OriginalParametersResponse originalParametersResponse) {
                if (isRequestSuccess){

                    if (originalParametersResponse.resultCode==0){
                        ToastUtil.show(R.string.Set_success);
                        updateLocalData((HashMap<String,Serializable>)originalParametersResponse.originalParameters);
                        finish();

                    }else {
                        ToastUtil.show(R.string.Modify_Failed);
                    }

                }else {
                    ToastUtil.show(R.string.Network_connection_error);
                }


            }
        });
    }

    private void updateLocalData(HashMap<String, Serializable> originalParameters) {
        Set<String> sets = originalParameters.keySet();
        for (String k:sets){
            Serializable serializable = originalParameters.get(k);
            if (TextUtils.equals(HttpsParameters.HEIGHT,k)){
                //说明更改了身高
                UserSpUtil.writeInt(Constants.IUser.HEIGHT, (int) serializable);
                ScaleUser.getUser().setHeight((int) serializable);
                originalHeight=(int) serializable;
            }
        }
    }
}
